package com.poh.controller;

import com.poh.pojo.Reply;
import com.poh.pojo.User;
import com.poh.service.ReplyService;
import com.poh.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import java.util.List;

@Controller
public class ReplyController {
    @Autowired
    ReplyService replyService;
    @Autowired
    UserService userService;
    @PostMapping("/addReply")
    public String addReply(Reply reply, Model model, HttpSession session) {
        User user = (User) session.getAttribute("user");
        System.out.println(reply.getTopicId());
        reply.setUserId(user.getUserId());
        replyService.addReply(reply);
        return "redirect:/intoTopic/" + reply.getTopicId();
    }
    @PostMapping("/replyUser")
    public String replyUser(Reply reply, Model model, HttpSession session) {
        User user = (User) session.getAttribute("user");
        System.out.println("被回复者" + reply.getUserId());
        System.out.println("回复者" + user.getUserId());
        reply.setBeReplyedUserName(userService.getUserNameById(reply.getUserId()));
        reply.setUserId(user.getUserId());
        replyService.addReply(reply);
        return "redirect:/intoTopic/" + reply.getTopicId();
    }
    @GetMapping("/toMyReply")
    public String toMyReply(HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        List<Reply> replies = replyService.queryReplyByUserId(user.getUserId());
        model.addAttribute("replies", replies);
        return "/topic/myReply";
    }
    @GetMapping("/deleteMyReply/{replyId}")
    public String deleteReply(@PathVariable("replyId") int replyId) {
        replyService.deleteReplyById(replyId);
        return "redirect:/toMyReply";
    }
    @GetMapping("/toMyReplyed")
    public String toMyReplyed(HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        List<Reply> replies = replyService.queryReplyedByReplyedName(user.getUserName());
        model.addAttribute("replies", replies);
        return "/topic/myReplyed";
    }
}
